package org.zjvis.datascience.common.widget.vo;

import lombok.Data;
import lombok.ToString;
import org.zjvis.datascience.common.widget.constant.WordTemplateConstant;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;

/**
 * @description TODO
 * @date 2022/2/18
 */
@Data
@ToString
public class NetImage {
    private String url;

    private Integer width;

    private Integer height;

    private String base64Code;

    public NetImage(String url){
        this.url = url;
        this.base64Code = WordTemplateConstant.DEFAULT_IMG;
    }

    /**
     * 返回一个默认的图片
     */
    public NetImage(){
        this.url = "http://localhost:8080/icon/components/barChart.png";
        this.width = 350;
        this.height = 168;
        this.base64Code = WordTemplateConstant.DEFAULT_IMG;
    }

    public void read(BufferedImage image, String imgSuffix) throws IOException {
        this.setWidth(image.getWidth());
        this.setHeight(image.getHeight());
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(image, imgSuffix, os);
        this.setBase64Code(Base64.getEncoder().encodeToString(os.toByteArray()));
    }

}